<template>
	<view class="app">
		<view class="cell" style="display: flex;justify-content: space-between;">
			<text class="tit fill">{{$t('user.name25')}}</text>
			<view class="avatar-wrap" @click="chooseImage">
				<image class="avatar" :src="tempAvatar || userInfo.avatar || '/static/icon/default-avatar.png'" mode="aspectFill"></image>
				<!-- 进度遮盖 -->
				<view class="progress center" :class="{
						'no-transtion': uploadProgress === 0,
						show: uploadProgress != 100
					}"
				 :style="{
						width: uploadProgress + '%',
						height: uploadProgress + '%',
					}"></view>
			</view>
		</view>
		<view class="cell">
			<text class="tit fill">{{$t('user.name26')}}</text>
			<input class="input" v-model="userInfo.username" type="text" disabled :placeholder="$t('user.name26')"
			 placeholder-class="placeholder">
		</view>
		<view class="cell">
			<text class="tit fill">{{$t('home.name9')}}</text>
			<input class="input" v-model="userInfo.nickname" type="text"  :placeholder="$t('user.name27')" placeholder-class="placeholder">
		</view>
		<!-- <button class="add-btn" @click="confirm('add')" style="background-color: #fa436a !important;">保存资料</button> -->
		<view style="height: 100rpx;"></view>
		<button class="add-btn" style="background: #1E84F1;color: #FFFFFF;margin:5px 15px" @click="confirm('add')">{{$t('user.name28')}}</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				uploadProgress: 100, //头像上传进度
				tempAvatar: '',
				userInfo: {},
				dateString: '',
				type: 'date',
			}
		},
		computed: {
			indicatorStyle() {
				return {
					background: 'rgba(255,0,0,.2)',
					height: '40px',
				};
			}
		},
		onLoad() {
			this.$fun.ajax.post('user/index', {}).then(res => {
				if (res.code == 1) {
					this.userInfo = res.data
				}
			})
			uni.setNavigationBarTitle({
				title:this.$t('user.name24')
			})
		},
		methods: {
			/**
			 * isShowToolBar：true 点击确定获取数值
			 * isShowToolBar：false 滑动获取返回数据
			 */
			dateTimeChange(value) {
				this.userInfo.birthday = value
			},
			//提交修改
			async confirm() {
				const that =this
				const {
					userInfo,
				} = this;
				if (!userInfo.avatar && userInfo.avatar.slice(0, 10) != 'data:image') {
					this.$fun.msg(this.$t('user.name29'));
					// this.$refs.confirmBtn.stop();
					return;
				}
				if (!userInfo.nickname) {
					this.$fun.msg(this.$t('user.name27'));
					// this.$refs.confirmBtn.stop();
					return;
				}
				let userDate = {
					avatar: userInfo.avatar,
					nickname: userInfo.nickname,
				}
				const res = await this.$fun.ajax.post('/user/profile',{ ...userDate
				}).then(res => {
					if (res.code == 1) {
						this.$fun.msg(this.$t('user.name30'))
						setTimeout(()=>{
							that.$fun.jump('/pages/user/user',3)
						},1200)
					}
				})
			},
			//选择头像
			chooseImage() {
				let _this = this;
				uni.chooseImage({
					count: 1,
					success: res => {
						uni.showLoading({
							title: this.$t('user.name33'),
						});
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: this.$baseUrl + '/api/common/upload', // 仅为示例，非真实的接口地址 
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								token: uni.getStorageSync('token'),
								contentType: "application/json"
							},
							success(res) {
								var res = JSON.parse(res.data)
								_this.userInfo.avatar = res.data.fullurl
								uni.showToast({
									title: this.$t('user.name31'),
									icon: "none"
								});
								setTimeout(() => {
									uni.hideLoading()
								}, 1000);
							},
						})
					}
				});
			},
			//裁剪回调
			async setAvatar(filePath) {
				this.tempAvatar = filePath;
				this.uploadProgress = 0;
				const result = await uniCloud.uploadFile({
					filePath: filePath,
					cloudPath: +new Date() + ('000000' + Math.floor(Math.random() * 999999)).slice(-6) + '.jpg',
					onUploadProgress: e => {
						this.uploadProgress = Math.round(
							(e.loaded * 100) / e.total
						);
					}
				});
				if (!result.fileID) {
					this.$util.msg(this.$t('user.name32'));
					return;
				}
				if (typeof uniCloud.getTempFileURL === 'undefined') {
					this.userInfo.avatar = result.fileID;
				} else {
					const tempFiles = await uniCloud.getTempFileURL({
						fileList: [result.fileID]
					})
					const tempFile = tempFiles.fileList[0];
					if (tempFile.code === 'SUCCESS') {
						this.userInfo.avatar = tempFile.download_url;
					} else {
						this.$util.msg(this.$t('user.name32'));
					}
				}
			},
			//修改性别
			changeGender(gender) {
				this.userInfo.gender = gender
			},
			//公开信息
			onSwitch(e) {
				this.userInfo.anonymous = !e.detail.value;
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background: rgb(255, 255, 255);
	}

	.app {
		padding-top: 16rpx;
	}

	.cell {
		display: flex;
		align-items: center;
		min-height: 110rpx;
		margin: 0 40rpx;
		border-bottom: 1px solid rgb(236, 236, 236);

		&:first-child {
			margin-bottom: 10rpx;
		}

		&:after {
			left: 40rpx;
			right: 40rpx;
			border-color: #d8d8d8;
		}

		.tit {
			font-size: 30rpx;
			color: #333;
		}

		.avatar-wrap {
			width: 120rpx;
			height: 120rpx;
			position: relative;
			border-radius: 100rpx;
			overflow: hidden;

			.avatar {
				width: 100%;
				height: 100%;
				border-radius: 100rpx;
			}

			.progress {
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				width: 100rpx;
				height: 100rpx;
				box-shadow: rgba(0, 0, 0, .6) 0px 0px 0px 2005px;
				border-radius: 100rpx;
				transition: .5s;
				opacity: 0;

				&.no-transtion {
					transition: 0s;
				}

				&.show {
					opacity: 1;
				}
			}
		}

		.input {
			flex: 1;
			text-align: right;
			font-size: 28rpx;
			color: #333;
		}

		switch {
			margin: 0;
			transform: scale(0.8) translateX(10rpx);
			transform-origin: center right;
		}

		.tip {
			margin-left: 20rpx;
			font-size: 28rpx;
			color: #999;
		}

		.checkbox {
			padding: 12rpx 0 12rpx 40rpx;
			font-size: 28rpx;
			color: #333;

			.mix-icon {
				margin-right: 12rpx;
				font-size: 36rpx;
				color: #ccc;
			}

			.icon-xuanzhong {
				color: $base-color;
			}
		}
	}
</style>
